package com.kry.mbs.config;

import java.util.Properties;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.FreeMarkerRender;
import com.kry.mbs.config.routes.AdminRoutes;
import com.kry.mbs.config.routes.MainRoutes;
import com.kry.mbs.interceptor.DemoInterceptor;
import com.kry.mbs.model.Blog;
import com.kry.mbs.model.Bookingrecord;
import com.kry.mbs.model.Commercial;
import com.kry.mbs.model.Manager;
import com.kry.mbs.model.Meetingroom;
import com.kry.mbs.model.PartnerThirdMatch;
import com.kry.mbs.model.Trade;


public class MainConfig extends JFinalConfig{

	@Override
	public void configConstant(Constants me) {
		me.setEncoding("utf-8");
		me.setDevMode(true);
		//me.setBaseViewPath("/WEB-INF/content");//页面模板根路径
		me.setFreeMarkerViewExtension(".html");//freemarker 模板后缀名
	}

	@Override
	public void configRoute(Routes me) {
		me.add(new AdminRoutes()); //
		me.add(new MainRoutes());  //
	}

	@Override
	public void configPlugin(Plugins plugins) {
		Prop p = PropKit.use("config.properties");
		DruidPlugin druidPlugin = new DruidPlugin(p.get("jdbcUrl"),p.get("user"),p.get("password"));
		plugins.add(druidPlugin);
		
		ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(druidPlugin);
		activeRecordPlugin.addMapping("blog","id", Blog.class);//
		activeRecordPlugin.addMapping("manager", "id", Manager.class);
		activeRecordPlugin.addMapping("bookingrecord", "id", Bookingrecord.class);
		activeRecordPlugin.addMapping("meetingroom", "id", Meetingroom.class);
		
		
//		activeRecordPlugin.addMapping("commercial","commercialID", Commercial.class);//
//		activeRecordPlugin.addMapping("partner_third_match","id", PartnerThirdMatch.class);//
//		activeRecordPlugin.addMapping("trade","id", Trade.class);//
		
		plugins.add(activeRecordPlugin);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		  // 添加控制层全局拦截器 
	      me.addGlobalActionInterceptor(new DemoInterceptor()); 
	      // 添加业务层全局拦截器 
	    //  me.addGlobalServiceInterceptor(new DemoInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("base"));//添加项目contextPath,以便在页面直接获取该值 ${base?if_exists}
	}
	
	 public void afterJFinalStart() {  
	      Properties p = loadPropertyFile("freemarker.properties");//由于我们用到freemarker，所以在此进行freemarker配置文件的装载  
	      FreeMarkerRender.setProperties(p);  
	    }  

}
